package io.seata.samples.tcc.springboot.dubbo.action.impl;

import io.seata.core.context.RootContext;
import io.seata.rm.tcc.TwoPhaseResult;
import io.seata.rm.tcc.api.BusinessActionContext;
import io.seata.samples.tcc.springboot.dubbo.action.TccActionTwo;
import io.seata.samples.tcc.springboot.dubbo.service.ActionTwoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

/**
 * The type Tcc action two.
 *
 * @author louis
 */
@Service
public class TccActionTwoImpl implements TccActionTwo {

    @Reference(version = "1.0.0", group = "tcc")
    private ActionTwoService actionTwoService;

    @Override
    public TwoPhaseResult prepare(String param) {
        String xid = RootContext.getXID();
        String result = ":::: TccActionTwo prepare, xid:" + xid + ", param:" + param;
        System.out.println(result);
        String msg = actionTwoService.prepare(param);  // RPC调用操作二服务
        return new TwoPhaseResult(true, result + "\n" + msg);
    }

    @Override
    public TwoPhaseResult commit(BusinessActionContext actionContext) {
        String xid = actionContext.getXid();
        String result = ":::: TccActionTwo commit, xid:" + xid + ", param:" + actionContext.getActionContext("param");
        System.out.println(result);
        String msg = actionTwoService.commit(xid);  // RPC调用操作二服务
        return new TwoPhaseResult(true, result + "\n" + msg);
    }

    @Override
    public TwoPhaseResult rollback(BusinessActionContext actionContext) {
        String xid = actionContext.getXid();
        String result = ":::: TccActionTwo rollback, xid:" + xid  + ", param:" + actionContext.getActionContext("param");
        System.out.println(result);
        String msg = actionTwoService.rollback(xid);  // RPC调用操作二服务
        return new TwoPhaseResult(true, result + "\n" + msg);
    }

}
